﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Graph
{
    public partial class frmMain : Form
    {
        private BaseGraph graph = null;
        protected const int THRESHOLD_LENGTH_DISPLAY_UI = 100000;

        public frmMain()
        {
            InitializeComponent();
        }

        private void chooseFile_button_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Title = "Open File";
            DialogResult result = openDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.fileName_textBox.Text = openDialog.FileName;
                initGraph();
            }
        }

        private void btnChooseOutputDir_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtOutputDir.Text = fbd.SelectedPath;
            }
        }

        private void chkOutToFile_CheckedChanged(object sender, EventArgs e)
        {
            txtOutputDir.Enabled = btnChooseOutputDir.Enabled = chkOutToFile.Checked;
        }

        private void initGraph()
        {
            if (this.directed_radioButton.Checked == true)
            {
                graph = new DirectedGraph(this.fileName_textBox.Text);
            }
            else
            {
                graph = new UndirectedGraph(this.fileName_textBox.Text);
            }
        }

        private void run_button_Click(object sender, EventArgs e)
        {
            int start = 0;
            if (!string.IsNullOrWhiteSpace(this.fileName_textBox.Text))
            {
                start = Environment.TickCount;
                initGraph();
            }
            else
            {
                MessageBox.Show("Choose file before run", null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            List<Graph.BaseGraph.Cycle> cycles = graph.findAllCycles();
            if (chkOutToFile.Checked)
            {
                string filePath = Path.Combine(txtOutputDir.Text, "GRAPH.OUT");
                graph.printAllCyclesToFile(filePath);
            }
            int duration = Environment.TickCount - start;
            txtRunningTime.Text = duration.ToString("#,##0");
            printCycle(cycles);
        }

        private void printCycle(List<Graph.BaseGraph.Cycle> cycles)
        {
            this.cycles_richTextBox.Text = "";
            if (cycles == null || cycles.Count <= 0)
            {
                this.cycles_richTextBox.Text = "0\n";
            }
            else
            {
                int maxCapcity = cycles.Count * graph.getNVertexes() / 2;
                if (maxCapcity > THRESHOLD_LENGTH_DISPLAY_UI) maxCapcity = THRESHOLD_LENGTH_DISPLAY_UI;
                StringBuilder lines = new StringBuilder(maxCapcity);
                lines.AppendLine(cycles.Count.ToString("#,##0"));
                foreach (Graph.BaseGraph.Cycle cycle in cycles)
                {
                    lines.AppendLine(cycle.ToString());
                    if (lines.Length >= THRESHOLD_LENGTH_DISPLAY_UI)
                    {
                        lines.Append("...");
                        break;
                    }
                }
                this.cycles_richTextBox.Text = lines.ToString();
            }
        }
    }
}
